<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>草稿列表</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <link rel="stylesheet" href="css/flatpickr.min.css">
    <script src="js/flatpickr.js"></script>
</head>
<style>
    .container{
            width: 1300px;
            margin-left: (100%-1300px);
        }
</style>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
   
        <a href="blog_list.html">主页</a>
        <a href="myblog_list.html">我的文章</a>
        <a href="personal_center.html">个人中心</a>
        <a href="javascript:logout()">注销</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="img/doge.jpg" id="photo" class="avtar" alt="">
                <h3 id="username"></h3>
                <a href="http:www.github.com" id="github">github 地址</a>
                <div class="counter">
                    <span>草稿</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span id="artCount">0</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div id="artDiv" class="container-right">
    
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <div class="blog">
                <div class="title">草稿题目</div>
                <div class="date">2021-06-02</div>
                <div class="desc">
                    从今天起, 我要认真敲代码
                </div>
                <a href="blog_content.html?id=1" class="detail">查看全文 &gt;&gt;</a>
                <a href="blog_content.html?id=1" class="detail">修改 &gt;&gt;</a>
                <a href="blog_content.html?id=1" class="detail">删除 &gt;&gt;</a>
                <a href="blog_content.html?id=1" class="detail">发布 &gt;&gt;</a>
                定时发布：<input id="myID" type="text" class="calendar" >
                <button onclick="mysub()">确定</button>
            </div>
           
        </div>

        <div class="content" id="contentright">
           
        </div>
    </div>

    <script>

        //手动初始化日历插件
        function initDatePicker(ele){
            ele.flatpickr({
                altFormat:"Y-m-d H:i:S",
                altInput:true,
                local:'zh',
                enableTime:true,
                enableSeconds:true,
                time_24hr:true
            });
        }
        initDatePicker($(".calendar"));
        

        // document.getElementById("myID").flatpickr();
        // $("#myID").flatpickr(); 

        //获取左侧个人信息
        function showInfo(){
            jQuery.ajax({
                url:"/user/showdraftinfo",   //草稿左侧个人信息需要修改
                type:"POST",
                data:{},
                success:function(result){
                    if(result!=null && result.code == 200){
             
                        jQuery("#photo").attr("src",result.data.photo);
                        jQuery("#github").attr("href",result.data.github);
                        jQuery("#github").text(result.data.github);
                        jQuery("#username").text(result.data.username); 
                        jQuery("#artCount").text(result.data.artCount); 

                    }else{
                        alert("个人信息加载失败，请刷新");
                    }
                }
            });
        }
        showInfo();


        //显示草稿信息
        function getMyArtList(){
            jQuery.ajax({
                url:"/art/mydraft",
                type:"POST",
                data:{},
                success:function(result){
                    if(result!=null && result.code==200){
                        //两种情况，一种发表了文章，一种没有发表文章
                        if(result.data!=null && result.data.length>0){
                            //用户发表了文章
                            //每次循环
                            var artListDiv = "";
                            for(var i=0;i<result.data.length;i++){
                                var artItem = result.data[i];
                                artListDiv += '<div class="blog">';
                                artListDiv += '<div class="title">' + artItem.title+'</div>';
                                artListDiv += '<div class="date">' + artItem.updatetime+'</div>';
                                artListDiv += '<div class="desc">';
                                artListDiv += artItem.content;
                                artListDiv += '</div>';
                                artListDiv += '<a href="mydraft_content.html?id='+artItem.id + ' " class="detail"> 查看全文 &gt;&gt;</a> ';
                                artListDiv += '<a href="mydraft_edit.html?id='   +artItem.id + ' " class="detail"> 修改 &gt;&gt;</a> ';
                                artListDiv += '<a href="javascript:myDel('    +artItem.id + '); " class="detail"> 删除 &gt;&gt;</a> ';
                                artListDiv += '<a href="javascript:mysub('    +artItem.id + '); " class="detail"> 发布 &gt;&gt;</a> ';
                                artListDiv += '定时发布：<input id="myID'+ artItem.id +'" type="text" class="calendar" > ';
                                artListDiv += '<button onclick="mysub1('    +artItem.id + ')">确定</button> ';
                                artListDiv += '</div>';

                            }
                            jQuery("#artDiv").html(artListDiv);
                            initDatePicker($('.blog>input'));
                            

                        }else{
                            //当前用户没有发表任何文章
                            jQuery("#artDiv").html("<h3>暂无草稿<h3>");
                        
                        }



                    }else{
                        alert("查询失败，请重试");
                    }
                }
            });

        }
        getMyArtList();

        //删除草稿
        function myDel(id){
            if(confirm("确实是否删除？")){
                // 删除文章
                jQuery.ajax({
                    url:"/art/deldraft",
                    type:"POST",
                    data:{"id":id},
                    success:function(result){
                        if(result!=null && result.code==200 && result.data==1){
                            alert("删除成功");
                            location.href = location.href;
                        }else{
                            alert("删除失败，请重试");
                        }

                    }
                });
            }
        }

          // 发布草稿
          function mysub(id){
            if(confirm("确认提交")){
                //1.id合法性校验
                //不能输入当前时间之前的时间

    
                //2.进行博客添加操作
                jQuery.ajax({
                    url:"/art/adddraftById",
                    type:"POST",
                    data:{"id":id},
                    success:function(result){
                        if(result!=null && result.code==200 && result.data==1){
                            if(confirm("添加文章成功")){
                                //刷新当前页面
                                location.href = location.href;
                            }

                        }else{
                            alert("抱歉，文章添加失败，请重试");
                        }
                    }
                });

            }
        }

            // 定时发布草稿
            function mysub1(id){
                //1.id合法性校验
                var time1 = "myID"+id;
                if(document.getElementById(time1).value==""){
                    alert("请选择您何时发布文章");
                    // myID.focus();
                    alert("日历：")
                    return;
                }

                //不能选择比当前时间早的时间
                const start = new Date(document.getElementById(time1).value).getTime();
                const date = new Date().getTime();
                if(start <= date){
                    alert("请选择当前时间之后的日期");
                    return;
                }


            if(confirm("确认在"+document.getElementById(time1).value+"时发布吗？")){
                //2.进行博客添加操作
                jQuery.ajax({
                    url:"/art/adddraftByIdTime",
                    type:"POST",
                    data:{"id":id,"time":document.getElementById(time1).value},
                    success:function(result){
                        if(result!=null && result.code==200 && result.data==1){
                            if(document.getElementById(time1).value!=""){
                                alert("文章将在"+document.getElementById(time1).value+"发布");
                                location.href = location.href;
                            }

                        }else{
                            alert("抱歉，文章添加失败，请重试");
                        }
                    }
                });

            }
        }

 
        //退出登录
        function logout(){
            if(confirm("确认注销？")){
                jQuery.ajax({
                    url:"/art/logout",
                    type:"POST",
                    data:{},
                    success:function(result){
                        if(result!=null && result.code == 200){
                            location.href = "/login.html";
                        }
                    }
                });
            }
        }



    </script>




</body>

</html>